<?php

Class Model_LSI_Category {
	const TABLE = "lsi_category";

	/**
	 * @var protected $word word value
	 * @method string get_word() get_word(void)
	 * @method void set_word() set_word(string $word)
	 */
	private $word;

	/**
	 * @var protected $context context value
	 * @method string get_context() get_context(void)
	 * @method void set_context() set_context(string $context)
	 */
	private $context;

	/**
	 * @var protected $category_id category_id value
	 * @method int get_category_id() get_category_id(void)
	 * @method void set_category_id() set_category_id(int $category_id)
	 */
	private $category_id;

	/**
	 * @var protected $count count value
	 * @method int get_count() get_count(void)
	 * @method void set_count() set_count(int $count)
	 */
	public $count = "0";

	/**
	 * @return Model_LSI_Category
	 */
	public function __construct($word, $context, $category_id) {
		$this->word = $word;
		$this->context = $context;
		$this->category_id = $category_id;
		if($obj = DB::select("count")->from(self::TABLE)->where("word", "=", $word)->where("context", "=", $context)->where("category_id", "=", $category_id)->limit(1)->as_object()->execute()->current()) {
			$this->count = $obj->count;
		}
	}

	public function save() {
		if (1 >= $this->count) {
			$db = DB::insert(self::TABLE, array("word", "context", "category_id", "count"))->values(array($this->word, $this->context, $this->category_id, 1));
		} else {
			$db = DB::update(self::TABLE)->set(array("count" => $this->count))->where("word", "=", $this->word)->where("context", "=", $this->context)->where("category_id", "=", $this->category_id);
		}
		$db->execute();
	}

}